package com.qf.interceptors;

import com.qf.cache.MenuCache;
import com.qf.entity.SysMenu;
import com.qf.mapper.SysMenuDao;
import com.qf.mapper.SysMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author ztf
 * @Date 15:21 2022/1/10
 * api拦截
 **/
@Component
public class ApiCheckInter implements HandlerInterceptor {
    private MenuCache menuCache;

    @Autowired
    public void setMenuCache(MenuCache menuCache) {
        this.menuCache = menuCache;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求地址
        String requestURI = request.getRequestURI();
        //查看地址是否被禁用
        SysMenu menu = menuCache.getMenuByUri(requestURI);
        if (menu != null) {
            //查询是否启用
            String enable = menu.getEnable();
            if (!"1".equals(enable)) {
                response.getWriter().write("intercept");
            }
            return "1".equals(enable);
        }
        return true;
    }
}
